function step01() {
    $("#flag").html("正在开启摄像头")
}

function step02() {
    $("#flag").html("请正视前方")
}

function openMedia() {
    let constraints = {
        video: {width: 500, height: 500},
        audio: false
    };
    //获得video摄像头
    let video = document.getElementById('video');
    let promise = navigator.mediaDevices.getUserMedia(constraints);
    promise.then((mediaStream) => {
        mediaStreamTrack = typeof mediaStream.stop === 'function' ? mediaStream : mediaStream.getTracks()[1];
        video.srcObject = mediaStream;
        video.play();
    });
}

// 拍照
function step03(url) {
    //获得Canvas对象
    let video = document.getElementById('video');
    let canvas = document.getElementById('canvas');
    let ctx = canvas.getContext('2d');
    ctx.drawImage(video, 0, 0, 500, 500);

    let img = document.getElementById('canvas').toDataURL();// toDataURL  图片数据流对象  默认'image/jpeg'

    $("#flag").html("正在识别");
    postData(url,{"imgBase64": img},true).then((res)=>{
        if (res.code === 200) {
            window.location.href = "/page/index.html";
            closeMedia()
        } else {
            window.location.reload();
        }
    })
}

// 关闭摄像头
function closeMedia() {
    mediaStreamTrack.stop();
}